package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class AdminMainFrameController implements Initializable {
    @FXML
    private Label changeicon;
    @FXML
    private Label lookCdicon;
    @FXML
    private Label historyIcon;
    @FXML
    private Label manageIcon;

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();              // 组件初始化
        addListener();                // 添加事件监听
    }

    // 组件初始化
    private void componentInit(){

        // 给标签设置icon图标
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 32, 32, false, false);
        changeicon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/cdIcon.png"), 64, 64, false, false);
        lookCdicon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/HistoryIcon.png"), 64, 64, false, false);
        historyIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/manageIcon.png"), 84, 84, false, false);
        manageIcon.setGraphic(new ImageView(image));
    }

    // 添加监听事件
    private void addListener(){
        // 调用自定义工具类改变鼠标进入控件是的形状为手型
        WindowUtils.changeMouse(changeicon);
        WindowUtils.changeMouse(lookCdicon);
        WindowUtils.changeMouse(historyIcon);
        WindowUtils.changeMouse(manageIcon);
        // 调用自定义工具类改变在执行耗时操作时鼠标的形状为旋转的圈
        WindowUtils.MouseWait(manageIcon);

        // 给图标添加鼠标监听事件 根据鼠标的位置来改变图标的大小
        changeicon.addEventHandler(MouseEvent.MOUSE_ENTERED, e ->{
            Image image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 40, 40, false, false);
            changeicon.setGraphic(new ImageView(image));
        });
        changeicon.addEventHandler(MouseEvent.MOUSE_EXITED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/exitandchange.png"), 32, 32, false, false);
            changeicon.setGraphic(new ImageView(image));
        });

        lookCdicon.addEventHandler(MouseEvent.MOUSE_ENTERED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/cdIcon.png"), 80, 80, false, false);
            lookCdicon.setGraphic(new ImageView(image));
        });
        lookCdicon.addEventHandler(MouseEvent.MOUSE_EXITED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/cdIcon.png"), 64, 64, false, false);
            lookCdicon.setGraphic(new ImageView(image));
        });
        historyIcon.addEventHandler(MouseEvent.MOUSE_ENTERED, e->{
            Image iamge = new Image(getClass().getResourceAsStream("/icon/HistoryIcon.png"), 80, 80, false, false);
            historyIcon.setGraphic(new ImageView(iamge));
        });
        historyIcon.addEventHandler(MouseEvent.MOUSE_EXITED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/HistoryIcon.png"), 64, 64, false, false);
            historyIcon.setGraphic(new ImageView(image));
        });
        manageIcon.addEventHandler(MouseEvent.MOUSE_ENTERED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/manageIcon.png"), 96, 96, false, false);
            manageIcon.setGraphic(new ImageView(image));
        });
        manageIcon.addEventHandler(MouseEvent.MOUSE_EXITED, e->{
            Image image = new Image(getClass().getResourceAsStream("/icon/manageIcon.png"), 84, 84, false, false);
            manageIcon.setGraphic(new ImageView(image));
        });
    }

    // 显示管理CD窗口
    @FXML
    private void look() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/LookCdFrame.fxml", "/icon/loginIcon.png", "查询光盘");
        WindowUtils.destroyStage(lookCdicon);          // 销毁当前窗口
    }

    // 显示借阅历史窗口
    @FXML
    private void history() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/HistoryFrame.fxml", "/icon/loginIcon.png", "借阅记录");
        WindowUtils.destroyStage(historyIcon);
    }

    // 显示管理用户界面
    @FXML
    private void manage() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/UserManageFrame.fxml", "/icon/loginIcon.png", "用户管理");
        WindowUtils.destroyStage(manageIcon);
    }

    // 改变登录用户 切换回登录界面
    @FXML
    private void change() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/LoginFrame.fxml", "/icon/loginIcon.png", "请登录");
        WindowUtils.destroyStage(changeicon);
    }
}
